/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.nii.qlks.GUI;

import edu.nii.qlks.ControlLayer.LoaiPhongControl;
import edu.nii.qlks.DataTransferObject.LoaiPhongInfo;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author 10520_000
 */
public class jfrm_ThemLoaiPhong extends javax.swing.JInternalFrame {

    /**
     * Creates new form jfrm_ThemLoaiPhong
     */
    LoaiPhongControl loaiphongCT;
    LoaiPhongInfo loaiphongInfo;

    public jfrm_ThemLoaiPhong() {
        initComponents();
        loaiphongCT = new LoaiPhongControl();
        loaiphongInfo = new LoaiPhongInfo();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jLabel_TieuDe = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jTextField_TenLoaiPhong = new javax.swing.JTextField();
        jTextField_DonGia = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jButton_LamMoi = new javax.swing.JButton();
        jButton_Them = new javax.swing.JButton();
        jButton_Thoat = new javax.swing.JButton();

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));

        jLabel_TieuDe.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel_TieuDe.setText("THÊM LOẠI PHÒNG");

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Thông tin phòng", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 12))); // NOI18N

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel2.setText("Tên Loại Phòng");

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel3.setText("Đơn giá");

        jTextField_TenLoaiPhong.setToolTipText("Nhập tên loại phòng");
        jTextField_TenLoaiPhong.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTextField_TenLoaiPhong_Click(evt);
            }
        });

        jTextField_DonGia.setToolTipText("Nhập đơn giá phòng");
        jTextField_DonGia.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTextField_DonGia_Click(evt);
            }
        });
        jTextField_DonGia.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField_DonGiaActionPerformed(evt);
            }
        });

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel4.setText("VND");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(37, 37, 37)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2))
                .addGap(82, 82, 82)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jTextField_TenLoaiPhong, javax.swing.GroupLayout.DEFAULT_SIZE, 243, Short.MAX_VALUE)
                    .addComponent(jTextField_DonGia))
                .addGap(18, 18, 18)
                .addComponent(jLabel4)
                .addContainerGap(54, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextField_TenLoaiPhong, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(29, 29, 29)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField_DonGia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4))
                .addContainerGap(104, Short.MAX_VALUE))
        );

        jButton_LamMoi.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jButton_LamMoi.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/refresh.png"))); // NOI18N
        jButton_LamMoi.setText("Làm mới");
        jButton_LamMoi.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_LamMoiActionPerformed(evt);
            }
        });

        jButton_Them.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jButton_Them.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/add.png"))); // NOI18N
        jButton_Them.setText("Thêm");
        jButton_Them.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_ThemActionPerformed(evt);
            }
        });

        jButton_Thoat.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jButton_Thoat.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/exit_16.png"))); // NOI18N
        jButton_Thoat.setText("Đóng");
        jButton_Thoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_ThoatActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(235, 235, 235)
                        .addComponent(jLabel_TieuDe))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(62, 62, 62)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(67, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(jButton_LamMoi)
                .addGap(29, 29, 29)
                .addComponent(jButton_Them, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButton_Thoat, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(169, 169, 169))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addComponent(jLabel_TieuDe)
                .addGap(34, 34, 34)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(28, 28, 28)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton_LamMoi, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton_Them, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton_Thoat, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(30, Short.MAX_VALUE))
        );

        getContentPane().add(jPanel2, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTextField_TenLoaiPhong_Click(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTextField_TenLoaiPhong_Click
        // TODO add your handling code here:
        jTextField_TenLoaiPhong.setText(null);
    }//GEN-LAST:event_jTextField_TenLoaiPhong_Click

    private void jTextField_DonGia_Click(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTextField_DonGia_Click
        // TODO add your handling code here:
        jTextField_DonGia.setText(null);
    }//GEN-LAST:event_jTextField_DonGia_Click

    private void jButton_LamMoiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_LamMoiActionPerformed
        // TODO add your handling code here:
        jTextField_DonGia.setEnabled(true);
        jTextField_TenLoaiPhong.setEnabled(true);
        jButton_Them.setEnabled(true);
        jTextField_TenLoaiPhong.setText(null);
        jTextField_DonGia.setText(null);
    }//GEN-LAST:event_jButton_LamMoiActionPerformed

    private void jButton_ThemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_ThemActionPerformed
        if(jTextField_TenLoaiPhong.getText().isEmpty() && jTextField_DonGia.getText().isEmpty())
        {
            JOptionPane.showMessageDialog(this, "Tên phòng & Đơn giá phòng không được để trống", "Lỗi", JOptionPane.ERROR_MESSAGE);
            
        }
        else if(jTextField_TenLoaiPhong.getText().isEmpty()){
            JOptionPane.showMessageDialog(this, "Tên phòng không được để trống", "Lỗi", JOptionPane.ERROR_MESSAGE);
        }
        else if(jTextField_DonGia.getText().isEmpty()){
            JOptionPane.showMessageDialog(this, "Đơn giá phòng không được để trống", "Lỗi", JOptionPane.ERROR_MESSAGE);
        }
        else{
            loaiphongInfo.setTenLoaiPhong(jTextField_TenLoaiPhong.getText());
//            Character keychar;
//            keychar = jTextField_DonGia.getText().charAt(WIDTH);
//            if (keychar.isDigit(keychar))
            try
            {
                loaiphongInfo.setDonGiaPhong(Float.parseFloat(jTextField_DonGia.getText()));
                /////////////////////////////////////////////////////
                Vector vt;
                try {
                    vt = loaiphongCT.LayDS(); // Lấy danh sách loại phòng
                    if (vt.size() > 0)
                    {
                        for (int i = 0; i < vt.size(); i++)
                        {
                            Vector vt_row = (Vector)vt.get(i);
                            String tenLoaiPhong = vt_row.get(1).toString().trim();
                            if (tenLoaiPhong.equalsIgnoreCase(jTextField_TenLoaiPhong.getText().trim()))
                            {
                                JOptionPane.showMessageDialog(rootPane, "Tên loại phòng đã tồn tại.", "Lỗi", JOptionPane.ERROR_MESSAGE);
                                jButton_LamMoiActionPerformed(evt);
                                jTextField_TenLoaiPhong.requestFocus();
                                return;
                            }
                        }
                        
                        Vector vt_row = (Vector)vt.get(vt.size() - 1); //Lấy dòng cuối cùng để thêm vào
                        String ma = vt_row.get(0).toString().trim();
                        loaiphongInfo.setMaLoaiPhong(loaiphongCT.TaoMaTuDong(ma, 3));
                    }
                    else{  //Chưa có loại phòng nào trong danh sách
                        loaiphongInfo.setMaLoaiPhong(loaiphongCT.TaoMaTuDong("MLP000", 3));
                    }
                    
                    loaiphongCT.Insert(loaiphongInfo);

                    JOptionPane.showMessageDialog(this, "Thêm thành công!", "Thông báo", JOptionPane.INFORMATION_MESSAGE);

                    jTextField_DonGia.setEnabled(false);
                    jTextField_TenLoaiPhong.setEnabled(false);
                    jButton_Them.setEnabled(false);
                    
                } catch (Exception ex) {
                    Logger.getLogger(jfrm_ThemLoaiPhong.class.getName()).log(Level.SEVERE, null, ex);
                    JOptionPane.showMessageDialog(this, "Thêm không thành công!", "Lỗi", JOptionPane.ERROR_MESSAGE);
                }
                
            }
            catch (Exception ex){
                JOptionPane.showMessageDialog(this, "Đơn giá chỉ được phép nhập số!", "Lỗi", JOptionPane.ERROR_MESSAGE);
                jTextField_DonGia.setText("");
            }

        }

    }//GEN-LAST:event_jButton_ThemActionPerformed

    private void jButton_ThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_ThoatActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jButton_ThoatActionPerformed

    private void jTextField_DonGiaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField_DonGiaActionPerformed
        jButton_ThemActionPerformed(evt);
    }//GEN-LAST:event_jTextField_DonGiaActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton_LamMoi;
    private javax.swing.JButton jButton_Them;
    private javax.swing.JButton jButton_Thoat;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel_TieuDe;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField jTextField_DonGia;
    private javax.swing.JTextField jTextField_TenLoaiPhong;
    // End of variables declaration//GEN-END:variables
}
